package utils;
import org.lwjgl.opengl.Display;


public class FPSCounter {
	
	private int frames = 0;
	private double unprocessedSeconds = 0;
	private long previousTime = System.nanoTime();
	private double secondsPerTick = 1 / 60.0;
	private int tickCount = 0;
	private boolean ticked = false;
	
	public FPSCounter() {
		
	}
	
	public void update(String title) {
		
		long currentTime = System.nanoTime();
		long passedTime = currentTime - previousTime;
		previousTime = currentTime;
		unprocessedSeconds += passedTime / 1000000000.0;
		
		while(unprocessedSeconds > secondsPerTick) {
			
			unprocessedSeconds -= secondsPerTick;
			ticked = true;
			tickCount++;
			if(tickCount % 60 == 0) {
				Display.setTitle(title + " - " + frames + " FPS");
				previousTime += 1000;
				
				frames = 0;
			}
		}
		if(ticked) {
			frames++;
		}
	}
}
